//
//  UnReadMenuViewController.m
//  myDemo2
//
//  Created by wuxueying on 5/12/14.
//
//

#import "UnReadMenuViewController.h"
#import "SCMenuViewController.h"
#import "SCOverlayPresentTransition.h"
#import "SCOverlayDismissTransition.h"
#import "SCDragAffordanceView.h"

@interface UnReadMenuViewController ()<UIScrollViewDelegate, UIViewControllerTransitioningDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) SCMenuViewController *menuViewController;
@property (nonatomic, strong) SCDragAffordanceView *menuDragAffordanceView;
@end
@implementation UnReadMenuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.menuViewController = [[SCMenuViewController alloc] initWithNibName:nil bundle:nil];
    self.menuViewController.transitioningDelegate = self;
    self.menuViewController.modalPresentationStyle = UIModalPresentationCustom;
    
    self.menuDragAffordanceView = [[SCDragAffordanceView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.scrollView.bounds) + 10.f, CGRectGetMidY(self.scrollView.bounds) - 25.f, 50.f, 50.f)];
    [self.scrollView addSubview:self.menuDragAffordanceView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.isDragging)
    {
        self.menuDragAffordanceView.progress = scrollView.contentOffset.x / CGRectGetWidth(self.menuDragAffordanceView.bounds);
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.menuDragAffordanceView.progress >= 1.f)
    {
        [self presentViewController:self.menuViewController
                           animated:YES
                         completion:NULL];
    }
    else
    {
        self.menuDragAffordanceView.progress = 0.f;
    }
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [[SCOverlayPresentTransition alloc] init];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [[SCOverlayDismissTransition alloc] init];
}

@end
